<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/13 0013
 * Time: 15:24
 */

namespace core;

use Curl\Curl;

class Solr
{
    //core name
    //索引名称
    public $core = 'lp';
    //solr q 全文检索
    public $q = 'q=*:*';
    //solr fq 过虑条件
    public $fq= '';
    //solr http rest接口
    public $url = '';
    //solr 排序
    public $sort = '';
    //solr 结果是否开户debug模式
    public $debug = false;
    //curl封装
    public static $_curl = '';

    /**
     * SOLR constructor. 析构函数
     * 初始化curl基类
     */
    public function __construct()
    {
        self::$_curl = new Curl();
    }

    /**
     * 设置索引名称 set core name
     * @param $core 索引名称
     * @return $this 链式操作
     */
    public function setCore($core)
    {
        $this->core = $core;
        return $this;
    }

    /**
     * 设置debug模式
     * @param bool $debug 是否开启debug模式
     */
    public function isDebug($debug = false)
    {
        $this->debug = $debug;
    }

    /**
     * 组合solr请求地址
     * @param string $queryStr 自定义全文检索条件 注意url转义引起异常
     */
    public function buildUrl($queryStr = '')
    {
        $host = config('solr.host');
        $port = config('solr.port');
        $this->url = "http://{$host}:{$port}/solr/{$this->core}/select?indent=on&wt=json";
        if ($this->debug) {
            $this->url .= "&debugQuery=on";
        }
        $this->url .= "&" . $this->q;
        !empty($this->fq) && $this->url .= $this->fq;
        !empty($this->sort) && $this->url .= "&" . $this->sort;
        !empty($queryStr) && $this->url .= "&" . urlencode($queryStr);
    }

    /**
     * 全文检索结果集排序
     * @param array $sortArr
     * @return $this
     */
    public function sort($sortArr = array())
    {
        if (!empty($sortArr)) {
            $this->sort = "sort=" . implode(", ", $sortArr);
        }
        return $this;
    }

    /**
     * 组合query条件
     * @param string $q solr全文检索 q条件
     * @return $this
     */
    public function q($q = '')
    {
        if (!empty($q)) {
            $this->q = "q=" . urlencode($q);
        }
        return $this;
    }

    /**
     * 组合filter query条件
     * @param array $fqArr solr全文检索 fq过虑条件
     * @return $this
     */
    public function fq($fqArr = array())
    {
        if (!empty($fqArr)) {
            foreach ($fqArr as $key => $value) {
                $this->fq .= "&fq={$key}:{$value}";
            }
        }
        return $this;
    }

    /**
     * 请求solr接口
     * @param int $start 开始位置
     * @param int $rows 行数
     * @param string $queryStr 其他solr全文检索条件
     * @return array
     */
    public function exec($start = 0, $rows = 20, $queryStr = '')
    {
        $this->buildUrl($queryStr);
        $this->url .= "&start={$start}&rows={$rows}";
        echo $this->url;

        $content = self::$_curl->get($this->url);
        return json_decode($content, true);
    }

    //
    /**
     * 静态函数式调用接口
     * @param string $queryStr 自定义全文检索条件 注意url转义
     * @param string $core 索引
     * @return array
     */
    public static function query($queryStr = "", $core = "lp")
    {
        $host = config('solr.host');
        $port = config('solr.port');
        $url = "http://{$host}:{$port}/solr/{$core}/select?indent=on&wt=json";
        $url .= "&" . $queryStr;
        echo $url;

        $content = self::$_curl->get($url);
        return json_decode($content, true);
    }
}